package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishList = dishMapper.selectByCategoryId(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (DishVO d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    //创建实体类对象
    //拷贝属性
    //完善数据
    //新增菜品,回写id
    //创建口味对象
    //遍历集合给dishid赋值
    @Override
    public void insert(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.insert(dish);
        log.info("新增菜品id为:{}", dish.getId().toString());

        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insert(flavors);
        }
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult selectByPage(DishPageQueryDTO dto) {
        //开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page = dishMapper.selectByPage(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //遍历集合根据id查询状态是否为起售
        Integer count = dishMapper.selectByDshId(ids);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        //判断是否与套餐表关联
        //根据dishid 查询出关联的套餐表信息
        List<Long> seleceByDishId = setmealDishMapper.selectByDishIds(ids);
        //删除关联的口味
        if (seleceByDishId != null && seleceByDishId.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
            dishMapper.deleteByIds(ids);
            dishFlavorMapper.deleteByDishIds(ids);

    }

    /**
     * 根据id查询菜品和对应口味
     *
     * @param id
     * @return
     */
    @Override
    public DishVO selectById(Long id) {
        Dish dish = dishMapper.selectById(id);
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    public void updateDish(DishDTO dishDTO) {
        //修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
        //删除口味
        dishFlavorMapper.deleteById(dishDTO.getId());
        //插入口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                //给逻辑外键绑定数据
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insert(flavors);
        }
        setmealDishMapper.updateWithDish(dishDTO);

    }

    /**
     * 根据分类id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishVO> selectByCategoryId(Long id,String name) {
        Dish dish = new Dish();
        dish.setCategoryId(id);
        dish.setName(name);
        dish.setStatus(StatusConstant.ENABLE);
        List<DishVO> list = dishMapper.selectByCategoryId(dish);
        return list;
    }

    /**
     * 修改状态
     *
     * @param status
     * @param id
     */
    @Override
    public void updateWithStutus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDish(dish);
    }
}
